package com.yiyi.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiyi.entity.Exam;
import com.yiyi.mapper.ExamMapper;
import com.yiyi.service.ExamService;
import com.yiyi.vo.ExamPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yiyi
 * @date 2021/5/31
 */
@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    /**
     * 保存新试卷
     *
     * @param exam
     * @return
     * */
    @Override
    public int save(Exam exam) {
        return examMapper.insert(exam);
    }

    /**
     * 根据positionId获取试卷
     *
     * @param positionId
     * @return
     * */
    @Override
    public Exam findByPositionId(String positionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("position_id", positionId);
        List<Exam> exams = examMapper.selectList(queryWrapper);
        if (exams.size() > 1) {
            // 存在多张试卷，随机返回一张
            int i = RandomUtil.randomInt(0, exams.size());
            return exams.get(i);
        } else if (exams.size() == 1) {
            // 只有一张试卷
            return exams.get(0);
        }
        // 不存在该职位对应的试卷
        return null;
    }

    /**
     * 根据id获取试卷
     *
     * @param id
     * @return
     * */
    @Override
    public Exam findById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("exam_id", id);
        return examMapper.selectOne(queryWrapper);
    }

    /**
     * 分页获取试卷
     *
     * @param current
     * @param size
     * @return
     * */
    @Override
    public ExamPageVO findByPage(Integer current, Integer size) {
        ExamPageVO examPageVO = new ExamPageVO();
        IPage<Exam> page = new Page<>(current, size);
        page = examMapper.selectPage(page, null);
        examPageVO.setCurrent(current);
        examPageVO.setSize(size);
        examPageVO.setPages(page.getPages());
        examPageVO.setTotal(page.getTotal());
        examPageVO.setExams(page.getRecords());
        return examPageVO;
    }

    /**
     * 根据岗位id获取对应的试卷id列表
     *
     * @param positionId
     * @return
     * */
    @Override
    public List<String> findIdsByPositionId(String positionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("position_id", positionId);
        List<Exam> exams = examMapper.selectList(queryWrapper);

        List<String> ids = new ArrayList<>();
        for (Exam exam : exams) {
            ids.add(exam.getExamId());
        }
        return ids;
    }

    /**
     * 删除试卷
     *
     * @param id
     * @return
     * */
    @Override
    public int deleteById(String id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("exam_id", id);
        return examMapper.delete(queryWrapper);
    }
}
